package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.EmpCertificateAddDTO;
import org.jsola.hr.dto.EmpCertificateUpdateDTO;
import org.jsola.hr.query.EmpCertificateQuery;
import org.jsola.hr.service.IEmpCertificateService;
import org.jsola.hr.vo.EmpCertificateListVO;
import org.jsola.hr.vo.EmpCertificateVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * @author zhr
 */
@RestController("hrEmpCertificateController")
@RequestMapping(HrConstants.API_PATH + "/v1/empCertificates")
public class EmpCertificateController implements IEmpCertificateControllerAPI {

    @Autowired
    private IEmpCertificateService empCertificateService;

    @Override
    @PostMapping
    public Result<EmpCertificateVO> save(@Valid @RequestBody EmpCertificateAddDTO empCertificateAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增员工证书权限校验

        return Result.success(
                empCertificateService.save(empCertificateAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody EmpCertificateUpdateDTO empCertificateUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改员工证书权限校验

        int count = empCertificateService.update(empCertificateUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{empCertificateId}")
    public Result<Integer> delete(@PathVariable String empCertificateId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除员工证书权限校验

        int count = empCertificateService.deleteByIds(tokenUser, empCertificateId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] empCertificateIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除员工证书权限校验

        int count = empCertificateService.deleteByIds(tokenUser, empCertificateIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{empCertificateId}")
    public Result<EmpCertificateVO> findById(@PathVariable String empCertificateId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看员工证书权限校验

        return Result.success(
            empCertificateService.selectById(empCertificateId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<EmpCertificateListVO>> listAll(@Valid EmpCertificateQuery empCertificateQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看员工证书权限校验

        return Result.success(
            empCertificateService.select(empCertificateQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<EmpCertificateListVO>> page(@Valid EmpCertificateQuery empCertificateQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            empCertificateService.selectPage(empCertificateQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @DeleteMapping(value = "/deleteEmpCertificate/{empCertificateId}")
    public Result<Integer> deleteEmpCertificate(@PathVariable String empCertificateId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除员工培训经历权限校验

        Integer count = empCertificateService.deleteEmpCertificate(tokenUser, empCertificateId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }


}

